var Utils = new Object();

Utils.trim = function(text) {
    if (typeof (text) == "string")
    {
        return text.replace(/^\s*|\s*$/g, "");
    } else {
        return text;
    }
};

Utils.isEmpty = function(val)
{
    switch (typeof (val)) {
        case 'string':
            return Utils.trim(val).length == 0 ? true : false;
            break;
        case 'number':
            return val == 0;
            break;
        case 'object':
            return val == null;
            break;
        case 'array':
            return val.length == 0;
            break;
        default:
            return true;
    }
};

Utils.isInt = function(val) {
    if (val == "") {
        return false;
    }
    var reg = /\D+/;
    return !reg.test(val);
};

Utils.isEmail = function(email) {
    var reg1 = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
    return reg1.test(email);
};

/*加法函数，用来得到精确的加法结果
 *返回值：arg1加上arg2的精确结果
 */
Utils.mathAdd = function(arg1, arg2) {
    var r1, r2, m;
    try {
        r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
};

/*减法函数
 *返回值：arg2减arg1的精确结果
 */
Utils.mathSub = function(arg2, arg1) {
    var r1, r2, m, n;
    try {
        r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    //last modify by deeka
    //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return ((arg2 * m - arg1 * m) / m).toFixed(n);
};

/*乘法函数，用来得到精确的乘法结果
 *返回值：arg1乘以arg2的精确结果
 */
Utils.mathMul = function(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try {
        m += s1.split(".")[1].length;
    } catch (e) {
    }
    try {
        m += s2.split(".")[1].length;
    } catch (e) {
    }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
};

/*除法函数，用来得到精确的除法结果
 *返回值：arg1除以arg2的精确结果
 */
Utils.mathDiv = function(arg1, arg2) {
    var t1 = 0, t2 = 0, r1, r2;
    try {
        t1 = arg1.toString().split(".")[1].length;
    } catch (e) {
    }
    try {
        t2 = arg2.toString().split(".")[1].length;
    } catch (e) {
    }
    with (Math) {
        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        return (r1 / r2) * pow(10, t2 - t1);
    }
};

/**
 * 设置cookie
 * @param objName cookie名称
 * @param objValue cookie值
 * @param objDays 保存时间，单位为天数
 */
Utils.setCookie = function(objName, objValue, objDays) {//添加cookie
    var str = objName + "=" + escape(objValue);
    if (objDays > 0) {//为0时不设定过期时间，浏览器关闭时cookie自动消失
        var date = new Date();
        var ms = objDays * 86400000;
        date.setTime(date.getTime() + ms);
        str += ";expires=" + date.toGMTString();
    }
    document.cookie = str;
};

/**
 * 获取cookie
 * @param {type} name
 * @returns {unresolved}
 */
Utils.getCookie = function(c_name) {
    if (document.cookie.length > 0) {
        var arr = document.cookie.match(new RegExp("(^| )" + c_name + "=([^;]*)(;|$)"));
        if (arr !== null)
            return unescape(arr[2]);
    }
    return null;
};

//删除指定名字的cookie
Utils.deleteCookie = function(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");

        if (c_start != -1) {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval = Utils.getCookie(c_name);
            document.cookie = c_name + "=" + cval + "; expires=" + exp.toGMTString();
        }
    }
};
